package com.hzhc.controller.yworder;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelStyleType;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzhc.common.BaseController;
import com.hzhc.common.FileExcelUtil;
import com.hzhc.entity.YwOrderShip;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.YwOrderCntrService;
import com.hzhc.service.YwOrderShipService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.vo.YwOrderCntrVo;
import com.hzhc.vo.YwOrderShipVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import static cn.afterturn.easypoi.excel.ExcelExportUtil.SHEET_NAME;

/**
 * 散改集船舶信息 前端控制器
 *
 * @author zhoumx
 * @since 2024-08-01
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/ywOrderShip")
public class YwOrderShipController extends BaseController {
    private final YwOrderShipService ywOrderShipService;
    private final YwOrderCntrService ywOrderCntrService;

    /**
     * 查询
     */
    @RequestMapping("/getOrderShipList")
    public ResultUtil<List<YwOrderShipVo>> getOrderList(@RequestBody YwOrderShipVo vo) {
        Page<YwOrderShipVo> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<YwOrderShipVo> vos = ywOrderShipService.getOrdershipPage(page, vo);
        return ResultUtil.build(vos,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }
    /**
     * 更新
     */
    @RequestMapping("/updateOrderShipList")
    public ResultUtil<T> updateOrderShipList(@RequestBody YwOrderShip vo) {
        return ywOrderShipService.updataOrdership(vo);
    }

    private final static String EXPORT_EXCEL_PATH = "static/download/散改集船舶统计.xlsx";

    @RequestMapping("/exportTableData")
    public String getTableExportExcel(HttpServletRequest request, HttpServletResponse response
            , YwOrderShipVo vo) {
        List<YwOrderShipVo> vos = ywOrderShipService.getOrdership(vo);
        LinkedList<Map<String, Object>> dataList=new LinkedList<>();
        LinkedList<Map<String, Object>> dataList1=new LinkedList<>();
        Map<String,Object> map=new HashMap<>();
        map.put("data",vos);
        map.put(SHEET_NAME,"船名航次");
        dataList.add(map);
        for (YwOrderShipVo ywOrderShipVo : vos) {
            Map<String,Object> map1=new HashMap<>();
            List<YwOrderCntrVo>  cntrVos= ywOrderCntrService.getCntrByShip(ywOrderShipVo);
            map1.put("data",cntrVos);
            map1.put(SHEET_NAME,ywOrderShipVo.getShipName());
            dataList1.add(map1);
        }
        Map<Integer,List<Map<String, Object>>> mapList =new HashMap<>();
        mapList.put(0,dataList);
        mapList.put(1,dataList1);//这一步 为第二个模板的循环
        TemplateExportParams params = new TemplateExportParams(
                EXPORT_EXCEL_PATH, false);
        params.setHeadingRows(1);
        params.setHeadingStartRow(3);
        params.setStyle(ExcelStyleType.BORDER.getClazz());
        //生成多个sheet
        Workbook book= ExcelExportUtil.exportExcelClone(mapList, params);
        FileExcelUtil.downloadExcelIo(book, request, response,  "散改集船舶统计");
        return null;
    }
}
